using Avalonia.Controls;
using Avalonia.Platform.Storage;
using ReactiveUI;
using System.Linq;
using System.Reactive;
using System.Threading.Tasks;
using System;
using Avalonia.ReactiveUI;
using MySoftLab.DapFlash.ViewModels;
using Avalonia.Input;
using System.IO;

namespace MySoftLab.DapFlash.Views
{
    public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
    {
        public static MainWindow? Instance { get; private set; }

        public MainWindow()
        {
            InitializeComponent();
            
            Instance = this;

            this.WhenActivated(action =>
                action(ViewModel!.ShowFileSelectDialog.RegisterHandler(DoSelectFileAsync)));

            // hexEditor.HexView.set 
        }

        private async Task DoSelectFileAsync(IInteractionContext<Unit, string?> interaction)
        {
            var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
            {
                Title = "选择文件",
                AllowMultiple = false,
                FileTypeFilter = new[]
                {
                    new FilePickerFileType("Supported Files")
                    {
                        Patterns = new[] { "*.hex", "*.bin", "*.elf", "*.axf" }
                    },
                    new FilePickerFileType("All Files")
                    {
                        Patterns = new[] { "*.*" }
                    }
                }
            });

            if (files.Count > 0)
            {
                var selectedFile = files[0];
                interaction.SetOutput(selectedFile.Path.AbsolutePath);
            }
            else
            {
                interaction.SetOutput(null);
            }
        }

        protected override void OnClosing(WindowClosingEventArgs e)
        {
            base.OnClosing(e);
            
            if (DataContext is MainWindowViewModel viewModel)
            {
                viewModel.SaveSettings();
            }
            
            if (Instance == this)
            {
                Instance = null;
            }
        }

        private void OnDragOver(object? sender, DragEventArgs e)
        {
            // 检查拖拽的文件是否是我们支持的类型
            if (e.Data.Contains(DataFormats.Files))
            {
                var files = e.Data.GetFiles();
                if (files != null)
                {
                    var file = files.FirstOrDefault();
                    if (file != null)
                    {
                        var extension = Path.GetExtension(file.Path.LocalPath).ToLower();
                        if (extension is ".bin" or ".hex")
                        {
                            e.DragEffects = DragDropEffects.Copy;
                            return;
                        }
                    }
                }
            }
            e.DragEffects = DragDropEffects.None;
        }

        private async void OnDrop(object? sender, DragEventArgs e)
        {
            if (e.Data.Contains(DataFormats.Files))
            {
                var files = e.Data.GetFiles();
                if (files != null)
                {
                    var file = files.FirstOrDefault();
                    if (file != null)
                    {
                        var extension = Path.GetExtension(file.Path.LocalPath).ToLower();
                        if (extension is ".bin" or ".hex")
                        {
                            if (DataContext is MainWindowViewModel viewModel)
                            {
                                await viewModel.HandleFileOpen(file.Path.LocalPath);
                            }
                        }
                    }
                }
            }
        }
    }
}
